﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Remind.BLL;
using Remind.Model;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;

namespace Remind.View
{
    public partial class FrmReport : Form
    {
        private readonly ReportBLL rBLL = new ReportBLL();

        private readonly GroupBLL gBLL = new GroupBLL();

        public FrmReport()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            //如果姓名为空就查询所有的联系人信息
            if (txtCname.Text.Trim() == "")
            {
                //dgvReport.DataSourc
                dgvReport.DataSource = rBLL.GetAllInfo();
            }
            else
            {
                string cname = txtCname.Text.Trim();
                dgvReport.DataSource = rBLL.GetReportsLikeCname("%" + cname + "%");
            }
        }

        private void btntoday_Click(object sender, EventArgs e)
        {
            int month = DateTime.Now.Month;
            int day = DateTime.Now.Day;
            IList<ReportInfo> temp = new List<ReportInfo>();
            foreach (ReportInfo item in rBLL.GetAllInfo())
            {
                if (item.Cbirthday.Month == month && item.Cbirthday.Day == day)
                {
                    temp.Add(item);
                }
            }
            dgvReport.DataSource = temp;
        }

        /// <summary>  
        /// NPOI简单Demo，快速入门代码  
        /// </summary>  
        /// <param name="dtSource"></param>  
        /// <param name="strFileName"></param>  
        /// <remarks>NPOI认为Excel的第一个单元格是：(0，0)</remarks>  
        /// <Author>柳永法 http://www.yongfa365.com/ 2010-5-8 22:21:41</Author>  
        public static void ExportEasy(DataGridView dtSource, string strFileName)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();

            //填充表头  
            HSSFRow dataRow = (HSSFRow)sheet.CreateRow(0);
            for (int i = 0; i < dtSource.ColumnCount; i++)
            {
                dataRow.CreateCell(i).SetCellValue(dtSource.Columns[i].HeaderText);
            }

            StringBuilder sb = new StringBuilder();
            //填充内容  
            for (int i = 0; i < dtSource.Rows.Count; i++)
            {
                dataRow = (HSSFRow)sheet.CreateRow(i + 1);
                for (int j = 0; j < dtSource.Columns.Count; j++)
                {
                    if (j > 0)
                    {
                        dataRow.CreateCell(j).SetCellValue("\t");
                    }
                    if (dtSource.Rows[i].Cells[j].Value == null)
                        dataRow.CreateCell(j).SetCellValue("");
                    else
                        dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i].Cells[j].Value.ToString().Trim());
                }
            }

            //保存  
            using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
            {
                workbook.Write(fs);
            }
        }


        private void btnOutputExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                ExportEasy(dgvReport, dlg.FileName);
            }
        }

        private void FrmReport_Load(object sender, EventArgs e)
        {
            cmbGroup.DataSource = gBLL.GetGroup();
            cmbGroup.DisplayMember = "gname";
            cmbGroup.ValueMember = "gname";
            dgvReport.DataSource = rBLL.GetAllInfo();
        }

        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void cmbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            string goup = cmbGroup.SelectedValue.ToString();
            dgvReport.DataSource = rBLL.GetReportsByGname(goup);
        }

        private void recentlycomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int day = 7;
            //判断输入类型
            try
            {
                day = Convert.ToInt32(this.recentlycomboBox.Text);
            }
            catch (FormatException)
            {
                //清除面板上的sidebar
                MessageBox.Show("请输入1-31之间的数字。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            RecentlyCheck(day);
        }
        //查询最近过生日的朋友
        private void RecentlyCheck(int day)
        {
            //默认查询最近7天过生日的朋友
            IList<ReportInfo> allContacts = rBLL.GetAllInfo();
            IList<ReportInfo> recentlyContacts = new List<ReportInfo>();
            if (allContacts != null)
            {
                foreach (ReportInfo item in allContacts)
                {
                    //把出生日期转换成当前年
                    int age = DateTime.Now.Year - item.Cbirthday.Year;
                    DateTime nowBirthday = item.Cbirthday.AddYears(age);
                    //
                    int temp = nowBirthday.DayOfYear - DateTime.Now.DayOfYear;
                    if (temp <= day && temp >= 0)
                        recentlyContacts.Add(item);
                }
            }
            if (recentlyContacts.Count > 0)
            {
                dgvReport.DataSource = recentlyContacts;
            }
            else
            {
                MessageBox.Show("最近" + recentlycomboBox.Text + "天好像没人过生日哦", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dgvReport.DataSource = recentlyContacts;
            }
        }

        private void dateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            int month = dateTimePicker.Value.Month;
            int day = dateTimePicker.Value.Day;
            IList<ReportInfo> temp = new List<ReportInfo>();
            foreach (ReportInfo item in rBLL.GetAllInfo())
            {
                if (item.Cbirthday.Month == month && item.Cbirthday.Day == day)
                {
                    temp.Add(item);
                }
            }
            dgvReport.DataSource = temp;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {

        }
        //全选|取消全选
        private void ckbSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dgvReport.RowCount; i++)
            {
                bool ischecked = this.dgvReport.Rows[i].Cells[0].Value is bool;
                this.dgvReport.Rows[i].Cells[0].Value = !ischecked;
            }
        }
    }
}
